﻿using System;
using System.Collections.Generic;
using System.Text;
using SIOsystem.Model.OrderDataSetTableAdapters;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using System.ComponentModel;
using SIOsystem.Model.ReportDataSetTableAdapters;

namespace SIOsystem.Control
{
    class OrderClass
    {
        static public bool insertOrder(BindingList<OrderListClass> orderList, string InputName, long OID, ref long IOID)
        {
            try
            {
                if (orderList.Count>0)
                {
                    using (InsertInOrderTableAdapter db = new InsertInOrderTableAdapter())
                    {
                        OleDbConnection conn = db.Connection;
                        conn.Open();
                        OleDbTransaction tr = conn.BeginTransaction();
                        db.Transaction = tr;
                        object o = db.GetMaxSerials();
                        long maxSerials = 0;
                        if (o==null)
                        {
                            maxSerials = DateTime.Now.Year * 10000 + 1;
                        }
                        else
                        {
                            maxSerials = long.Parse(o.ToString());
                            maxSerials = Math.Max(DateTime.Now.Year * 10000 + 1, maxSerials+1);
                        }
                        long autoID = db.InsertInOrder(InputName, OID, maxSerials);
                        if (autoID > 0)
                        {
                            OleDbCommand cmd = conn.CreateCommand();
                            cmd.Transaction = tr;
                            cmd.CommandText = @"select @@identity";
                            autoID = long.Parse(cmd.ExecuteScalar().ToString());
                            IOID = autoID;
                        }
                        else
                        {
                            tr.Rollback();
                            return false;
                        }
                        int serialsCount = 0;
                        foreach (OrderListClass ol in orderList)
                        {
                            serialsCount += db.InsertInSerials(ol.Mid, ol.Pid, ol.Tid, ol.ProductName, ol.Amt, ol.Price, IOID, ol.Remark);
                        }
                        if (serialsCount==orderList.Count)
                        {
                            tr.Commit();
                            return true;
                        } 
                        else
                        {
                            tr.Rollback();
                            return false;
                        }
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public bool insertOrder(BindingList<OrderListClass> orderList, string OutputName, long DID, long OID)
        {
            try
            {
                if (orderList.Count > 0)
                {
                    using (InsertOutOrderTableAdapter db = new InsertOutOrderTableAdapter())
                    {
                        OleDbConnection conn = db.Connection;
                        conn.Open();
                        OleDbTransaction tr = conn.BeginTransaction();
                        db.Transaction = tr;
                        object o = db.GetMaxSerials();
                        long maxSerials = 0;
                        if (o == null)
                        {
                            maxSerials = DateTime.Now.Year * 10000 + 1;
                        }
                        else
                        {
                            maxSerials = long.Parse(o.ToString());
                            maxSerials = Math.Max(DateTime.Now.Year * 10000 + 1, maxSerials + 1);
                        }
                        long autoID = db.InsertOutOrder(OutputName, OID, DID, maxSerials);
                        if (autoID > 0)
                        {
                            OleDbCommand cmd = conn.CreateCommand();
                            cmd.Transaction = tr;
                            cmd.CommandText = @"select @@identity";
                            autoID = long.Parse(cmd.ExecuteScalar().ToString());
                        }
                        else
                        {
                            tr.Rollback();
                            return false;
                        }
                        int serialsCount = 0;
                        foreach (OrderListClass ol in orderList)
                        {
                            serialsCount += db.InsertOutSerials(ol.ISID, ol.Amt,autoID,ol.Remark);
                        }
                        if (serialsCount == orderList.Count)
                        {
                            tr.Commit();
                            return true;
                        }
                        else
                        {
                            tr.Rollback();
                            return false;
                        }
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public bool insertOrder(BindingList<OrderListClass> orderList, string InputName, long OID, long PID, ref long AOID)
        {
            try
            {
                if (orderList.Count > 0)
                {
                    using (InsertApplyOrderTableAdapter db = new InsertApplyOrderTableAdapter())
                    {
                        OleDbConnection conn = db.Connection;
                        conn.Open();
                        OleDbTransaction tr = conn.BeginTransaction();
                        db.Transaction = tr;
                        object o = db.GetMaxSerials();
                        long maxSerials = 0;
                        if (o == null)
                        {
                            maxSerials = DateTime.Now.Year * 10000 + 1;
                        }
                        else
                        {
                            maxSerials = long.Parse(o.ToString());
                            maxSerials = Math.Max(DateTime.Now.Year * 10000 + 1, maxSerials + 1);
                        }
                        long autoID = db.InsertApplyOrder(InputName, OID, maxSerials,PID);
                        if (autoID > 0)
                        {
                            OleDbCommand cmd = conn.CreateCommand();
                            cmd.Transaction = tr;
                            cmd.CommandText = @"select @@identity";
                            autoID = long.Parse(cmd.ExecuteScalar().ToString());
                            AOID = autoID;
                        }
                        else
                        {
                            tr.Rollback();
                            return false;
                        }
                        int serialsCount = 0;
                        foreach (OrderListClass ol in orderList)
                        {
                            serialsCount += db.InsertApplySerials(ol.Mid, ol.Pid, ol.Tid, ol.ProductName, ol.Amt, AOID, ol.Remark);
                        }
                        if (serialsCount == orderList.Count)
                        {
                            tr.Commit();
                            return true;
                        }
                        else
                        {
                            tr.Rollback();
                            return false;
                        }
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public long getMaxAppSerials()
        {
            try
            {
                using (InsertApplyOrderTableAdapter db = new InsertApplyOrderTableAdapter())
                {
                    return long.Parse(db.GetMaxProjectID().ToString());
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public DataTable getStorageList(long tid)
        {
            try
            {
                using (StorageTableAdapter db = new StorageTableAdapter())
                {
                    return db.GetSPListById(tid);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public DataTable getInOrder(long IOID)
        {
            try
            {
                using (InsertInOrderTableAdapter db = new InsertInOrderTableAdapter())
                {
                    DataTable dt = db.GetOrder(IOID);
                    string[] colString = { 
                                             "IOID", "编号"
                                           , "PName", "材料名" 
                                           , "TypeName", "规格" 
                                           , "InAmt", "数量" 
                                           , "Unit", "单位" 
                                           , "InPrice", "单价" 
                                           , "SSum", "金额" 
                                           , "Remark", "备注" 
                                           , "Serials", "单号" 
                                           , "InDate", "入库日期" 
                                         };
                    for (int i = 0; i < colString.Length / 2; i++)
                    {
                        dt.Columns[colString[2 * i]].SetOrdinal(i);
                        dt.Columns[i].ColumnName = colString[2 * i + 1];
                    }
                    while (dt.Columns.Count > colString.Length / 2)
                    {
                        dt.Columns.RemoveAt(colString.Length / 2);
                    }
                    return dt;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public DataTable getOutOrder(long IOID)
        {
            try
            {
                using (InsertOutOrderTableAdapter db = new InsertOutOrderTableAdapter())
                {
                    DataTable dt = db.GetOrder(IOID);
                    string[] colString = { 
                                             "OOID", "编号"
                                           , "PName", "材料名" 
                                           , "TypeName", "规格" 
                                           , "OutAmt", "数量" 
                                           , "Unit", "单位" 
                                           , "InPrice", "单价" 
                                           , "SSum", "金额" 
                                           , "Remark", "备注" 
                                           , "Serials", "单号" 
                                           , "ProjectID", "项目编号" 
                                           , "ProjectName", "关联项目" 
                                           , "OutDate", "出库日期" 
                                         };
                    for (int i = 0; i < colString.Length / 2; i++)
                    {
                        dt.Columns[colString[2 * i]].SetOrdinal(i);
                        dt.Columns[i].ColumnName = colString[2 * i + 1];
                    }
                    while (dt.Columns.Count > colString.Length / 2 )
                    {
                        dt.Columns.RemoveAt(colString.Length / 2 );
                    }
                    return dt;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public DataTable getAppOrder(long AOID)
        {
            try
            {
                using (InsertApplyOrderTableAdapter db = new InsertApplyOrderTableAdapter())
                {
                    DataTable dt = db.GetOrder(AOID);
                    string[] colString = { 
                                             "AOID", "编号"
                                           , "PName", "材料名" 
                                           , "TypeName", "规格" 
                                           , "AppAmt", "数量" 
                                           , "Unit", "单位" 
                                           , "Remark", "备注" 
                                           , "Serials", "单号" 
                                           , "ProjectID", "项目编号" 
                                           , "ProjectName", "项目名称" 
                                           , "ProjectPlace", "项目地点" 
                                           , "SendAddr", "送货地址" 
                                           , "Connecter", "接收人电话" 
                                           , "Payer", "费用结算对象" 
                                           , "AppDate", "申购日期" 
                                         };
                    for (int i = 0; i < colString.Length / 2; i++)
                    {
                        dt.Columns[colString[2 * i]].SetOrdinal(i);
                        dt.Columns[i].ColumnName = colString[2 * i + 1];
                    }
                    while (dt.Columns.Count > colString.Length / 2)
                    {
                        dt.Columns.RemoveAt(colString.Length / 2);
                    }
                    return dt;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public double[] getStorage(long tid)
        {
            try
            {
                using (StorageTableAdapter db = new StorageTableAdapter())
                {
                    DataTable dt = db.GetSPById(tid, tid);
                    if (dt.Rows.Count == 1)
                    {
                        return new double[] { Convert.ToDouble(dt.Rows[0]["库存"]), Convert.ToDouble(dt.Rows[0]["价格"]) };
                    }
                    else
                    {
                        return new double[] { 0, 0 };
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public double getCanOut(long ISID)
        {
            try
            {
                using (CanOutTableAdapter db = new CanOutTableAdapter())
                {
                    DataTable dt = db.GetCanOut(ISID);
                    if (dt.Rows.Count == 1)
                    {
                        return Convert.ToDouble(dt.Rows[0]["库存"]);
                    }
                    else
                    {
                        return 0;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public string[] getProductInfo(long tid)
        {
            try
            {
                using (StorageTableAdapter db = new StorageTableAdapter())
                {
                    DataTable dt = db.GetSPById(tid, tid);
                    if (dt.Rows.Count == 1)
                    {
                        return new string[] { dt.Rows[0]["库存"].ToString(), dt.Rows[0]["价格"].ToString(), dt.Rows[0]["单位"].ToString() };
                    }
                    else
                    {
                        return new string[] { "", "", "" };
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public int updateNewPrice(long isid, double newPrice)
        {
            try
            {
                using (CommandTableAdapter db = new CommandTableAdapter())
                {
                    return db.UpdateInPrice(newPrice, isid);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public bool updateAmtPrice(long sid, double oldPrice, double newPrice, int oldAmt, int outAmt, long ModifyOID, string remark)
        {
            try
            {
                using (CommandTableAdapter db = new CommandTableAdapter())
                {
                    int succ = db.UpdateAmtPrice(oldAmt - outAmt, newPrice, sid);
                    if (succ == 1)
                    {
                        db.InsertModifySerials(sid, oldPrice, newPrice, oldAmt, oldAmt - outAmt, "入库调整", ModifyOID, remark);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        static public bool updateOutAmt(long sid, double price,  int oldAmt, int outAmt, long ModifyOID, string remark)
        {
            try
            {
                using (CommandTableAdapter db = new CommandTableAdapter())
                {
                    int succ = db.UpateOutAmt(oldAmt - outAmt, sid);
                    if (succ == 1)
                    {
                        db.InsertModifySerials(sid, price, price, oldAmt, oldAmt - outAmt, "出库调整", ModifyOID, remark);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
    }
}
